typedef (*command)();

int getpid();
void ps();
void chname();
void kmode();
void kswitch();
int wait();
void exit();
void help();

char		commandNames[7][10] = { "ps", "chname", "kmode", "kswitch", "wait", "exit", "help" };
command		commands[7] = { ps, chname, kmode, kswitch, wait, exit, help };

void main()
{
	int pid = getpid(), i = 0;
	char cmd[20];
	while (1)
	{
		printf("\n*****************************************************\n");
		printf("In the user space! My PID = %d", pid);
		printf("\n*****************************************************\n");
		printf("Input Command: ");
		
		gets(cmd);
		
		printf("\n\n");
		for (i = 0; i < 7; ++i)
		{
			if (strcmp(commandNames[i], cmd) == 0)
			{
				(*commands[i])("");
				break;
			}
		}
	}
}

int getpid()
{
	return syscall(0, 0, 0, 0);
}

void ps()
{
	syscall(1, 0, 0, 0);
}

void chname()
{
	char newName[15];
	printf("What do you want to change it to: ");
	gets(newName);
	syscall(2, newName, 0, 0);
}

void kmode()
{
	syscall(3, 0, 0, 0);
}

void kswitch()
{
	syscall(4, 0, 0, 0);
}

int wait()
{
	int exitValue, pid;
	exitValue = syscall(5, 0, 0, 0);
	pid = getpid();
	printf("PID %d awoken with exit code %d\n", pid, exitValue);
}

void exit()
{
	char exitVal = 0;
	do
	{
		printf("  * Gimme exit value (0-9): ");
		exitVal = getc();
		printf("%c\n", exitVal);
	} while ((exitVal < '0') && (exitVal > '9'));
	syscall(6, (exitVal - '0'), 0, 0);
}

void help()
{
	printf("Commands -> | ps | chname | kmode | kswitch | wait | exit |\n");
}
